package com.m2h.xiaoke.api;


import android.content.Context;
import android.util.Log;

import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.VoiceWakeuper;
import com.iflytek.cloud.WakeuperListener;
import com.m2h.xiaoke.param.Constants;
import com.m2h.xiaoke.param.VoiceParamManager;

/**
 * @author lijh
 */
public class XunfeiAwaken  {

    private VoiceWakeuper voiceWakeuper;
    private Context mContext;
    private XunfeiAwaken() {
    }

    public static XunfeiAwaken getInstance() {
        return InstanceHolder.INSTANCE;
    }
    static class InstanceHolder {
        public static XunfeiAwaken INSTANCE = new XunfeiAwaken();
    }
    /**
     * 初始化
     * @param context
     */
    public void init(final Context context) {
        Log.d(Constants.TAG, "mSpeechAwaken init");
        mContext = context;
        voiceWakeuper = VoiceWakeuper.createWakeuper(context, new InitListener() {
            @Override
            public void onInit(int code) {
                //实际此处不执行，原因请看源码
            }
        });
    }

    /**
     * 反初始化
     */
    public void uninit() {
        voiceWakeuper = VoiceWakeuper.getWakeuper();
        if (voiceWakeuper != null) {
            voiceWakeuper.destroy();
        }
    }
    /**
     * 开启唤醒监听
     */
    public void startAwaken(WakeuperListener wakeuperListener) {
        Log.d(Constants.TAG,"startAwaken");
        VoiceParamManager.setAwakenParem(VoiceWakeuper.getWakeuper(),mContext);
        voiceWakeuper.startListening(wakeuperListener);
    }

    /**
     * 结束唤醒监听
     */
    public void stopAwaken() {
        voiceWakeuper.stopListening();
    }
}
